package com.jshcbd.mvp_kt_demo.utils

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.provider.Settings

object AppUtil {
    /**
     * 返回当前程序版本名
     *
     * @param context
     * @return
     */
    fun getAppVersionName(context: Context): String? {
        var versionName = ""
        var versioncode = 0
        try {
            // ---get the package info---
            val pm = context.packageManager
            val pi = pm.getPackageInfo(context.packageName, 0)
            pi.apply {
                versionName=this.versionName
                versioncode=this.versionCode
            }
            if (versionName.isEmpty()) {
                return ""
            }
        } catch (e: Exception) {
        }
        return versionName
    }

    /**
     * 获取当前应用的版本号
     *
     * @param context
     * @return
     * @throws Exception
     */
    fun getVersionName(context: Context): String {
        // 获取PackageManager的实例
        val packageManager = context.packageManager
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        val packInfo: PackageInfo?
        var version = ""
        try {
            packInfo = packageManager.getPackageInfo(context.packageName, 0)
            version = packInfo.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return version
    }

    /**
     * 获取手机IMSI
     *
     * @param context
     * @return
     */
    fun getAndroidId(context: Context): String {
        return Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
    }
}